$(function () {
    var registerurl = '/person/register';
    var sendcodeurl = '/person/sendPhoneCode';
    //点击发送验证码到指定手机中中
    $('#getcode').click(function () {
        var phone = $('#phone').val();
        if (!phone){
            $.toast('请输入手机号');
        } else {
            sendcodeurl = sendcodeurl+'?phone='+phone+'&type=register';
            time(this);
            $.getJSON(sendcodeurl,function (data) {
                if (data.success){
                    $.toast('发送成功');
                } else {
                    $.toast(data.message);
                }
            })
        }
    });

    //判断两次输入的密码是否一致
    $('#password2').on('change',function (e) {
        var password2 = e.target.value;
        var password1 = $('#password').val();
        if (password2!=password1){
            $(this).css('border','red 1px solid');
        } else {
            $(this).css('border','');
        }
    });


    $('#submit').click(function () {
        //分别获取注册的信息并存入personinfo中
        var password = $('#password').val();
        var phone = $('#phone').val();
        var vcode = $('#phone_code').val();
        if (password == null || phone == null || vcode == null
        || password == '' || phone =='' || vcode == '') {
            $.toast('填写信息不全');
        }else {
            var formData = new FormData();
            formData.append('password',password);
            formData.append('phone',phone);
            formData.append('code',vcode);
            $.ajax({
                url:registerurl,
                type:"POST",
                data:formData,
                contentType:false,
                processData:false,
                cache:false,
                success:function (data) {
                    $.toast(data.message);
                    if(data.success){
                        window.location.href = "/login";
                    }
                }
            })
        }
    });

    var wait=60;
    function time(o) {
        if (wait == 0) {
            o.removeAttribute("disabled");
            o.value="免费获取验证码";
            wait = 60;
        } else {
            o.setAttribute("disabled", true);
            o.value=wait+"秒后可以重新发送";
            wait--;
            setTimeout(function() {
                    time(o)
                },
                1000)
        }
    }


});
